package com.itheima.web.service.impl.pms;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.pms.AttrInfoDto;
import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.pms.PmsProductAttributeCategoryService;
import com.itheima.service.pms.PmsProductAttributeService;
import com.itheima.service.pms.PmsProductCategoryService;
import com.itheima.vo.pms.PmsProductAttributeCategory;
import com.itheima.vo.pms.ProductAttributeParam;
import com.itheima.vo.pms.ProductCategoryParam;
import com.itheima.web.mapper.pms.PmsProductAttributeMapper;
import com.itheima.web.mapper.pms.PmsProductCategoryMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, ProductAttributeParam> implements PmsProductAttributeService {
    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    @Autowired
    private PmsProductAttributeMapper pmsProductAttributeMapper;

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    /**
     * 根据商品分类的id获取商品属性及属性分类
     * @param productCategoryId
     * @return
     */
    @Override
    public ResultData attrInfo(Long productCategoryId) {
        AttrInfoDto attrInfoDto = new AttrInfoDto();
        Long attributeId = pmsProductAttributeMapper.getAttributeId(productCategoryId);
        if (attributeId != null) {
            attrInfoDto.setAttributeId(attributeId);
            LambdaQueryWrapper<ProductAttributeParam> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttributeParam::getId , attrInfoDto.getAttributeId());
            ProductAttributeParam productAttributeParam = pmsProductAttributeService.getOne(queryWrapper);
            attrInfoDto.setAttributeCategoryId(productAttributeParam.getProductAttributeCategoryId());
        }
        return ResultData.ok("操作成功" , attrInfoDto);
    }

    /**
     * 添加商品属性信息
     * @param productAttributeParam
     * @return
     */
    @Override
    @Transactional
    public void create(ProductAttributeParam productAttributeParam) {
        pmsProductAttributeService.save(productAttributeParam);
        LambdaQueryWrapper<PmsProductAttributeCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductAttributeCategory::getId , productAttributeParam.getProductAttributeCategoryId());
        PmsProductAttributeCategory pmsProductAttributeCategory = pmsProductAttributeCategoryService.getOne(queryWrapper);
        if (productAttributeParam.getType() == 0) {
            pmsProductAttributeCategory.setAttributeCount(pmsProductAttributeCategory.getAttributeCount() + 1);
            pmsProductAttributeCategoryService.updateById(pmsProductAttributeCategory);
        }else {
            pmsProductAttributeCategory.setParamCount(pmsProductAttributeCategory.getParamCount() + 1);
            pmsProductAttributeCategoryService.updateById(pmsProductAttributeCategory);
        }
    }

    /**
     * 批量删除商品属性
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        Long id = ids.get(0);
        ProductAttributeParam productAttributeParam = pmsProductAttributeService.getById(id);
        pmsProductAttributeService.removeByIds(ids);
        PmsProductAttributeCategory attributeCategoryServiceById = pmsProductAttributeCategoryService.getById(productAttributeParam.getProductAttributeCategoryId());
        if (productAttributeParam.getType() == 0) {
            attributeCategoryServiceById.setAttributeCount(attributeCategoryServiceById.getAttributeCount() - 1);
        }else {
            attributeCategoryServiceById.setParamCount(attributeCategoryServiceById.getParamCount() - 1);
        }
        pmsProductAttributeCategoryService.updateById(attributeCategoryServiceById);
    }

    /**
     * 根据分类查询属性列表或参数列表
     * @param cid
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultData listAttribute(Long cid, Integer type, Long pageNum, Long pageSize) {
        if (pageNum != null && pageSize != null) {
            Page<ProductAttributeParam> pageInfo = new Page<>(pageNum , pageSize);
            LambdaQueryWrapper<ProductAttributeParam> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttributeParam::getProductAttributeCategoryId , cid)
                    .eq(ProductAttributeParam::getType , type);
            pmsProductAttributeService.page(pageInfo , queryWrapper);
            PageResult pageResult = PageResult.init(pageInfo.getTotal(), pageSize, pageNum, pageInfo.getRecords());
            return ResultData.ok("操作成功" , pageResult);
        }else {
            LambdaQueryWrapper<ProductAttributeParam> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttributeParam::getProductAttributeCategoryId , cid)
                    .eq(ProductAttributeParam::getType , type);
            List<ProductAttributeParam> list = pmsProductAttributeService.list(queryWrapper);
            PageResult pageResult = new PageResult();
            pageResult.setList(list);
            return ResultData.ok("操作成功" , pageResult);
        }

    }

    /**
     * 修改商品属性信息
     * @param id
     * @param productAttributeParam
     * @return
     */
    @Override
    public void updateAttribute(Long id, ProductAttributeParam productAttributeParam) {
        productAttributeParam.setId(id);
        pmsProductAttributeService.updateById(productAttributeParam);
    }

    /**
     * 查询单个商品属性
     * @param id
     * @return
     */
    @Override
    public ProductAttributeParam getAttribute(Long id) {
        return pmsProductAttributeService.getById(id);
    }
}
